//
//  PxTabBar.m
//  PxBusWifi
//
//  Created by Xu Yefeng on 14-4-18.
//  Copyright (c) 2014年 Free. All rights reserved.
//

#import "PxTabBar.h"
#import "UIView+Sizes.h"
#import "LLSmallUtil.h"

@implementation PxTabBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)setImageNameList:(NSArray*)imageNameList
{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    float buttonWidth = self.width / imageNameList.count * 2;
    
    for (int i = 0; i < imageNameList.count; i += 2) {
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(i * buttonWidth / 2, 0, buttonWidth, self.height);
        
        [btn setImage:[UIImage imageNamed:imageNameList[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageNameList[i+1]] forState:UIControlStateHighlighted];
        [btn setImage:[UIImage imageNamed:imageNameList[i+1]] forState:UIControlStateSelected];
        
        [self addSubview:btn];
        btn.tag = 1000 + i / 2;
        
        [btn setSelector:@selector(btnSelected:) withTarget:self];
    }
    
    [self manualSetSelectIndex:0];
}

- (void)setBtnTitle:(NSArray*)titleList{
    
    for (int i = 0; i < titleList.count; i++) {
        NSString *title = titleList[i];
        
        UIButton *btn = (UIButton*)[self viewWithTag:1000 + i];
        
        if (btn != nil) {
            btn.titleLabel.font = [UIFont systemFontOfSize:12];
            [btn setTitle:title];
            [btn centerImageAndTitle];
        }
        
    }
}

- (void)btnSelected:(UIButton*)selectBtn
{
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton*)subview;
            
            if (selectBtn == btn) {
                
                btn.selected = YES;
                
            }
            else
            {
                btn.selected = NO;
            }
        }
    }
    
    NSInteger selectIndex = selectBtn.tag - 1000;
    if ([self.tabDelegate respondsToSelector:@selector(tabSelected:index:)]) {
        [self.tabDelegate tabSelected:self index:selectIndex];
        _currentIndex = selectIndex;  
    }
}

- (void)manualSetSelectIndex:(NSInteger)index
{
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton*)subview;
            
            if (btn.tag == index + 1000)
            {
                btn.selected = YES;
                _currentIndex = index;
            }
            else
            {
                btn.selected = NO;
            }
        }
    }
}
@end
